/**
 * 
 */
package dao.versant;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;

import javax.jdo.Query;

import dao.interfaces.ITrajetDAO;

import tp.model.Client;
import tp.model.Ticket;
import tp.model.Train;
import tp.model.Trajet;
import tp.model.Ville;

/**
 * @author Admin
 *
 */
public class TrajetDAOVersant extends VersantDAO<Trajet> implements ITrajetDAO {

	@Override
	public ArrayList<Trajet> findAllFromVille(Date date, Ville ville) {
		Query q = pm.newQuery(Trajet.class);
		q.declareParameters("java.util.Date d, String v");
		q.setFilter("date >= d && depart.nom == v");
		ArrayList<Trajet> listTrajets = new ArrayList<Trajet>();
		Collection collection;
		try {
			collection = (Collection) q.execute(date, ville.getNom());
			if (!collection.isEmpty()) {
				Iterator i = collection.iterator();
				while (i.hasNext()) {
					listTrajets.add((Trajet) i.next());
				}
			}
		} finally {
			if (listTrajets != null) q.close(listTrajets);
		}
		return listTrajets;
	}

	@Override
	public ArrayList<Trajet> findAllToVille(Date date, Ville ville) {
		Query q = pm.newQuery(Trajet.class);
		q.declareParameters("java.util.Date d, String v");
		q.setFilter("date >= d && arrivee.nom == v");
		ArrayList<Trajet> listTrajets = new ArrayList<Trajet>();
		Collection collection;
		try {
			collection = (Collection) q.execute(date, ville.getNom());
			if (!collection.isEmpty()) {
				Iterator i = collection.iterator();
				while (i.hasNext()) {
					listTrajets.add((Trajet) i.next());
				}
			}
		} finally {
			if (listTrajets != null) q.close(listTrajets);
		}
		return listTrajets;
	}
	
	@Override
	public ArrayList<Trajet> findAllFromVilleEtDistance(Date date, Ville ville,
			double distance) {
		Query q = pm.newQuery(Trajet.class);
		q.declareParameters("java.util.Date d, String v, double dist");
		q.setFilter("date >= d && depart.nom == v && distance == dist");
		ArrayList<Trajet> listTrajets = new ArrayList<Trajet>();
		Collection collection;
		try {
			collection = (Collection) q.execute(date, ville.getNom(), distance);
			if (!collection.isEmpty()) {
				Iterator i = collection.iterator();
				while (i.hasNext()) {
					listTrajets.add((Trajet) i.next());
				}
			}
		} finally {
			if (listTrajets != null) q.close(listTrajets);
		}
		return listTrajets;
	}

	@Override
	public ArrayList<Trajet> findAllByClient(String nomClient) {
		Query q = pm.newQuery(Client.class);
		q.declareParameters("String c");
		q.setFilter("nom == c");
		Client client = null;
		ArrayList<Trajet> listTrajets = new ArrayList<Trajet>();
		Collection collection;
		try {
			collection = (Collection) q.execute(nomClient);
			if (!collection.isEmpty()) {
				Iterator i = collection.iterator();
				if (i.hasNext())
					client = (Client) i.next();
			}
			for (Ticket t : client.getTickets()) {
				listTrajets.add(t.getTrajet());
			}
		} finally {
			if (client != null) q.close(client);
		}
		return listTrajets;
	}

	@Override
	public ArrayList<Trajet> findAllByTwoClient(String nomClient1, String nomClient2) {
		ArrayList<Trajet> listTrajetsClient1 = findAllByClient(nomClient1);
		ArrayList<Trajet> listTrajetsClient2 = findAllByClient(nomClient2);
		ArrayList<Trajet> listTrajetsGroup = new ArrayList<Trajet>();
		for (Trajet t : listTrajetsClient1) {
			if (listTrajetsClient2.contains(t)) {
				listTrajetsGroup.add(t);
			}
		}
		return listTrajetsGroup;
	}

	@Override
	public void updateReservation(Trajet trajet, Ticket ticket) {
		// TODO Auto-generated method stub
        pm.currentTransaction().begin();
        Trajet trajetPersistant = this.find(trajet);
        Ticket ticketPersistant = this.findTicket(ticket.getNom());
        trajetPersistant.getListTickets().add(ticketPersistant);
        pm.currentTransaction().commit();
	}
	
	private Ticket findTicket(String ticketNom) {
		// TODO Auto-generated method stub
		Query q = pm.newQuery(Ticket.class);
		q.declareParameters("String t");
		q.setFilter("nom == t");
		Ticket ticket = null;
		Collection collection;
		try {
			collection = (Collection) q.execute(ticketNom);
			if (!collection.isEmpty()) {
				Iterator i = collection.iterator();
				if (i.hasNext())
					ticket = (Ticket) i.next();
			}
		} finally {
			if (ticket != null)
				q.close(ticket);
		}
		return ticket;	
	}

	private Trajet find(Trajet trajet) {
		Query q = pm.newQuery(Trajet.class);
		q.declareParameters("String t");
		q.setFilter("nom == t");
		Trajet trajetPersistant = null;
		Collection collection;
		try {
			collection = (Collection) q.execute(trajet.getNom());
			if (!collection.isEmpty()) {
				Iterator i = collection.iterator();
				if (i.hasNext())
					trajetPersistant = (Trajet) i.next();
			}
		} finally {
			if (trajetPersistant != null) q.close(trajetPersistant);
		}
		return trajetPersistant;
	}
	
	@Override
	public void create(Trajet trajet) {
		pm.currentTransaction().begin();

		trajet.setTrain(this.findTrain(trajet.getTrain()));
		trajet.setDepart(this.findDepart(trajet.getDepart()));
		trajet.setArrivee(this.findArrivee(trajet.getArrivee()));
		
		pm.makePersistent(trajet);
		pm.currentTransaction().commit();
	}
	
	private Train findTrain(Train trainParam) {
		// TODO Auto-generated method stub
		Query q = pm.newQuery(Train.class);
		q.declareParameters("String t");
		q.setFilter("nom == t");
		Train train = null;
		Collection collection;
		try {
			collection = (Collection) q.execute(trainParam.getNom());
			if (!collection.isEmpty()) {
				Iterator i = collection.iterator();
				if (i.hasNext())
					train = (Train) i.next();
			}
		} finally {
			if (train != null)
				q.close(train);
		}
		return train;
	}

	private Ville findArrivee(Ville arrivee) {
		// TODO Auto-generated method stub
		Query q = pm.newQuery(Ville.class);
		q.declareParameters("String v");
		q.setFilter("nom == v");
		Ville ville = null;
		Collection collection;
		try {
			collection = (Collection) q.execute(arrivee.getNom());
			if (!collection.isEmpty()) {
				Iterator i = collection.iterator();
				if (i.hasNext())
					ville = (Ville) i.next();
			}
		} finally {
			if (ville != null)
				q.close(ville);
		}
		return ville;
	}

	private Ville findDepart(Ville depart) {
		// TODO Auto-generated method stub
		Query q = pm.newQuery(Ville.class);
		q.declareParameters("String v");
		q.setFilter("nom == v");
		Ville ville = null;
		Collection collection;
		try {
			collection = (Collection) q.execute(depart.getNom());
			if (!collection.isEmpty()) {
				Iterator i = collection.iterator();
				if (i.hasNext())
					ville = (Ville) i.next();
			}
		} finally {
			if (ville != null)
				q.close(ville);
		}
		return ville;
	}
	
	@Override
	public ArrayList<Trajet> findAll() {
		return null;
	}

	@Override
	public void delete(Trajet obj) {
		
	}

	@Override
	public void deleteReservation(Trajet trajet, Ticket ticket) {
		// TODO Auto-generated method stub
		
	}

}
